Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 660)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.96781 12.96717 12.96654 12.96591 12.96529 12.96467 12.96405 12.96343
##   [9] 12.96280 12.96216 12.96152 12.96086 12.96019 12.95950 12.95879 12.95807
##  [17] 12.95732 12.95655 12.95575 12.95492 12.95406 12.95316 12.95224 12.95127
##  [25] 12.95026 12.94921 12.94812 12.94698 12.94579 12.94456 12.94326 12.94192
##  [33] 12.94051 12.93905 12.93753 12.93594 12.93427 12.93250 12.93065 12.92871
##  [41] 12.92670 12.92461 12.92247 12.92026 12.91800 12.91570 12.91336 12.91099
##  [49] 12.90859 12.90617 12.90373 12.90129 12.89885 12.89641 12.89398 12.89157
##  [57] 12.88919 12.88683 12.88451 12.88224 12.88001 12.87783 12.87572 12.87367
##  [65] 12.87170 12.86981 12.86800 12.86629 12.86467 12.86316 12.86177 12.86024
##  [73] 12.85836 12.85614 12.85362 12.85080 12.84773 12.84441 12.84087 12.83713
##  [81] 12.83322 12.82916 12.82497 12.82067 12.81629 12.81185 12.80737 12.80287
##  [89] 12.79839 12.79393 12.78952 12.78519 12.78096 12.77685 12.77289 12.76908
##  [97] 12.76547 12.76207 12.75890 12.75599 12.75336 12.75104 12.74903 12.74738
## [105] 12.74609 12.74520 12.74432 12.74308 12.74153 12.73971 12.73766 12.73541
## [113] 12.73302 12.73051 12.72793 12.72532 12.72272 12.72017 12.71772 12.71540
## [121] 12.71325 12.71131 12.70963 12.70824 12.70719 12.70651 12.70625 12.70645
## [129] 12.70714 12.70838 12.71019 12.71262 12.71571 12.71950 12.72403 12.73023
## [137] 12.73882 12.74955 12.76216 12.77640 12.79200 12.80872 12.82629 12.84446
## [145] 12.86296 12.88156 12.89998 12.91797 12.93527 12.95163 12.96679 12.98049
## [153] 12.99248 13.00251 13.01276 13.02538 13.04000 13.05628 13.07384 13.09234
## [161] 13.11141 13.13070 13.14985 13.16849 13.18627 13.20284 13.21783 13.23089
## [169] 13.24165 13.24976 13.25707 13.26558 13.27517 13.28571 13.29706 13.30910
## [177] 13.32170 13.33473 13.34807 13.36157 13.37512 13.38858 13.40183 13.41473
## [185] 13.42717 13.43900 13.45010 13.46034 13.46960 13.47774 13.48463 13.49015
## [193] 13.49416 13.49655 13.49717 13.49590 13.49261 13.48717 13.47952 13.46977
## [201] 13.45811 13.44470 13.42971 13.41330 13.39565 13.37692 13.35728 13.33691
## [209] 13.31597 13.29463 13.27305 13.25141 13.22988 13.20862 13.18781 13.16760
## [217] 13.14818 13.12713 13.10218 13.07374 13.04217 13.00789 12.97126 12.93268
## [225] 12.89254 12.85122 12.80912 12.76662 12.72411 12.68197 12.64060 12.60039
## [233] 12.56172 12.52497 12.49055 12.45883 12.43020 12.40506 12.38081 12.35482
## [241] 12.32741 12.29890 12.26959 12.23982 12.20989 12.18013 12.15085 12.12236
## [249] 12.09499 12.06906 12.04487 12.02275 12.00301 11.98598 11.97179 11.96024
## [257] 11.95106 11.94400 11.93881 11.93525 11.93306 11.93199 11.93178 11.93220
## [265] 11.93298 11.93388 11.93464 11.93501 11.93475 11.93360 11.93131 11.92763
## [273] 11.92231 11.91742 11.91496 11.91458 11.91593 11.91867 11.92245 11.92691
## [281] 11.93171 11.93649 11.94092 11.94463 11.94728 11.94853 11.94802 11.94540
## [289] 11.94032 11.93426 11.92884 11.92398 11.91957 11.91554 11.91180 11.90823
## [297] 11.90477 11.90131 11.89777 11.89405 11.89006 11.88571 11.88091 11.87556
## [305] 11.86958 11.86287 11.85535 11.84691 11.83631 11.82262 11.80627 11.78769
## [313] 11.76730 11.74554 11.72281 11.69956 11.67621 11.65318 11.63090 11.60979
## [321] 11.59029 11.57281 11.55779 11.54564 11.53348 11.51839 11.50078 11.48106
## [329] 11.45964 11.43692 11.41331 11.38922 11.36507 11.34125 11.31817 11.29625
## [337] 11.27590 11.25751 11.24150 11.22828 11.21825 11.21183 11.20942 11.20974
## [345] 11.21122 11.21381 11.21747 11.22216 11.22784 11.23446 11.24199 11.25038
## [353] 11.25959 11.26958 11.28030 11.29173 11.30380 11.31649 11.32975 11.34477
## [361] 11.36264 11.38316 11.40614 11.43136 11.45863 11.48776 11.51854 11.55077
## [369] 11.58425 11.61878 11.65417 11.69021 11.72670 11.76345 11.80025 11.83690
## [377] 11.87321 11.90897 11.94399 11.97806 12.01099 12.04257 12.07261 12.10091
## [385] 12.12726 12.15480 12.18636 12.22134 12.25910 12.29903 12.34051 12.38291
## [393] 12.42561 12.46800 12.50945 12.54934 12.58705 12.62196 12.65344 12.68089
## [401] 12.70367 12.72424 12.74537 12.76690 12.78865 12.81048 12.83223 12.85372
## [409] 12.87480 12.89530 12.91508 12.93396 12.95179 12.96839 12.98363 12.99732
## [417] 13.00932 13.01945 13.02756 13.03349 13.03661 13.03663 13.03389 13.02872
## [425] 13.02146 13.01246 13.00204 12.99055 12.97832 12.96570 12.95302 12.94061
## [433] 12.92883 12.91800 12.90846 12.90055 12.89181 12.87978 12.86481 12.84727
## [441] 12.82750 12.80586 12.78270 12.75839 12.73327 12.70770 12.68204 12.65664
## [449] 12.63185 12.60803 12.58553 12.56472 12.54593 12.52954 12.51589 12.50273
## [457] 12.48778 12.47130 12.45360 12.43496 12.41567 12.39601 12.37628 12.35675
## [465] 12.33773 12.31950 12.30234 12.28654 12.27239 12.26018 12.25019 12.24148
## [473] 12.23293 12.22456 12.21640 12.20848 12.20083 12.19349 12.18647 12.17982
## [481] 12.17355 12.16771 12.16231 12.15739 12.15297 12.14909 12.14578 12.14306
## [489] 12.14097 12.13953 12.13885 12.13899 12.13989 12.14151 12.14380 12.14671
## [497] 12.15019 12.15420 12.15869 12.16361 12.16891 12.17454 12.18045 12.18660
## [505] 12.19294 12.19942 12.20706 12.21680 12.22847 12.24189 12.25690 12.27332
## [513] 12.29098 12.30970 12.32933 12.34968 12.37058 12.39186 12.41335 12.43488
## [521] 12.45628 12.47737 12.49798 12.51794 12.53707 12.55522 12.57220 12.58784
## [529] 12.60197 12.61442 12.62502 12.63359 12.64201 12.65211 12.66370 12.67655
## [537] 12.69046 12.70521 12.72059 12.73639 12.75240 12.76841 12.78421 12.79958
## [545] 12.81431 12.82820 12.84102 12.85257 12.86264 12.87102 12.87748 12.88183
## [553] 12.88386 12.88334 12.88006 12.87441 12.86698 12.85791 12.84734 12.83542
## [561] 12.82226 12.80803 12.79285 12.77687 12.76021 12.74303 12.72547 12.70765
## [569] 12.68971 12.67181 12.65407 12.63663 12.61964 12.60323 12.58412 12.55943
## [577] 12.52994 12.49642 12.45964 12.42037 12.37939 12.33747 12.29538 12.25389
## [585] 12.21377 12.17580 12.14074 12.10937 12.08247 12.06079 12.04128 12.02049
## [593] 11.99863 11.97594 11.95262 11.92891 11.90503 11.88119 11.85761 11.83452
## [601] 11.81214 11.79069 11.77039 11.75146 11.73412 11.71860 11.70511 11.69387
## [609] 11.68511 11.67761 11.67005 11.66256 11.65524 11.64818 11.64151 11.63533
## [617] 11.62974 11.62486 11.62078 11.61762 11.61548 11.61447 11.61470 11.61627
## [625] 11.61929 11.62335 11.62797 11.63319 11.63903 11.64552 11.65268 11.66055
## [633] 11.66915 11.67851 11.68866 11.69961 11.71141 11.72407 11.73762 11.75210
## [641] 11.76752 11.78392 11.80132 11.81975 11.83931 11.86005 11.88195 11.90496
## [649] 11.92906 11.95419 11.98034 12.00746 12.03552 12.06447 12.09430 12.12495
## [657] 12.15640 12.18860 12.22153 12.25514
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 660)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.59607 12.59294 12.58988 12.58690 12.58400 12.58117 12.57841 12.57573
##   [9] 12.57311 12.57056 12.56807 12.56565 12.56329 12.56099 12.55874 12.55656
##  [17] 12.55442 12.55234 12.55031 12.54833 12.54640 12.54451 12.54266 12.54086
##  [25] 12.53909 12.53737 12.53568 12.53403 12.53240 12.53081 12.52925 12.52772
##  [33] 12.52621 12.52472 12.52326 12.52182 12.52037 12.51887 12.51735 12.51580
##  [41] 12.51423 12.51264 12.51106 12.50947 12.50789 12.50632 12.50478 12.50327
##  [49] 12.50179 12.50035 12.49895 12.49762 12.49634 12.49514 12.49400 12.49295
##  [57] 12.49199 12.49112 12.49035 12.48970 12.48915 12.48873 12.48844 12.48828
##  [65] 12.48826 12.48839 12.48868 12.48913 12.48975 12.49054 12.49151 12.49261
##  [73] 12.49376 12.49496 12.49622 12.49755 12.49893 12.50038 12.50190 12.50348
##  [81] 12.50513 12.50686 12.50865 12.51052 12.51247 12.51450 12.51661 12.51879
##  [89] 12.52107 12.52343 12.52587 12.52841 12.53104 12.53376 12.53657 12.53949
##  [97] 12.54250 12.54561 12.54883 12.55215 12.55557 12.55910 12.56275 12.56650
## [105] 12.57037 12.57435 12.57815 12.58150 12.58445 12.58702 12.58928 12.59125
## [113] 12.59298 12.59452 12.59591 12.59719 12.59840 12.59958 12.60079 12.60205
## [121] 12.60342 12.60493 12.60664 12.60857 12.61078 12.61330 12.61619 12.61947
## [129] 12.62321 12.62743 12.63217 12.63750 12.64343 12.65088 12.66054 12.67223
## [137] 12.68572 12.70080 12.71726 12.73490 12.75349 12.77284 12.79273 12.81294
## [145] 12.83327 12.85351 12.87345 12.89287 12.91156 12.92932 12.94593 12.96118
## [153] 12.97487 12.98677 12.99975 13.01643 13.03625 13.05869 13.08318 13.10917
## [161] 13.13613 13.16350 13.19073 13.21727 13.24259 13.26612 13.28733 13.30566
## [169] 13.32056 13.33149 13.34038 13.34947 13.35870 13.36800 13.37729 13.38652
## [177] 13.39561 13.40449 13.41309 13.42135 13.42919 13.43655 13.44336 13.44955
## [185] 13.45505 13.45979 13.46370 13.46672 13.46877 13.46979 13.46970 13.46845
## [193] 13.46595 13.46215 13.45697 13.45034 13.44219 13.43247 13.42015 13.40450
## [201] 13.38585 13.36454 13.34088 13.31521 13.28787 13.25917 13.22945 13.19905
## [209] 13.16828 13.13748 13.10698 13.07711 13.04820 13.02057 12.99457 12.97051
## [217] 12.94874 12.92628 12.90022 12.87094 12.83882 12.80426 12.76765 12.72936
## [225] 12.68978 12.64930 12.60830 12.56718 12.52631 12.48609 12.44690 12.40912
## [233] 12.37315 12.33936 12.30815 12.27990 12.25499 12.23382 12.21456 12.19521
## [241] 12.17586 12.15661 12.13756 12.11879 12.10042 12.08252 12.06521 12.04857
## [249] 12.03270 12.01771 12.00367 11.99070 11.97889 11.96832 11.95998 11.95456
## [257] 11.95179 11.95136 11.95301 11.95643 11.96134 11.96746 11.97450 11.98216
## [265] 11.99017 11.99824 12.00607 12.01338 12.01988 12.02529 12.02933 12.03169
## [273] 12.03209 12.03361 12.03911 12.04800 12.05970 12.07362 12.08918 12.10579
## [281] 12.12287 12.13983 12.15609 12.17106 12.18415 12.19478 12.20237 12.20632
## [289] 12.20606 12.20308 12.19932 12.19480 12.18959 12.18372 12.17724 12.17021
## [297] 12.16265 12.15464 12.14619 12.13738 12.12823 12.11880 12.10914 12.09929
## [305] 12.08929 12.07919 12.06904 12.05889 12.04664 12.03053 12.01109 11.98884
## [313] 11.96431 11.93804 11.91055 11.88238 11.85405 11.82609 11.79903 11.77340
## [321] 11.74973 11.72856 11.71040 11.69579 11.68169 11.66496 11.64596 11.62508
## [329] 11.60268 11.57912 11.55479 11.53005 11.50528 11.48084 11.45711 11.43445
## [337] 11.41325 11.39386 11.37666 11.36202 11.35032 11.34192 11.33719 11.33469
## [345] 11.33276 11.33144 11.33077 11.33080 11.33157 11.33312 11.33550 11.33874
## [353] 11.34289 11.34800 11.35410 11.36124 11.36947 11.37882 11.38933 11.40208
## [361] 11.41794 11.43669 11.45812 11.48200 11.50811 11.53623 11.56613 11.59760
## [369] 11.63041 11.66435 11.69918 11.73469 11.77066 11.80687 11.84309 11.87911
## [377] 11.91469 11.94963 11.98369 12.01667 12.04832 12.07844 12.10681 12.13319
## [385] 12.15737 12.18184 12.20894 12.23827 12.26941 12.30195 12.33548 12.36958
## [393] 12.40385 12.43787 12.47123 12.50352 12.53432 12.56322 12.58981 12.61369
## [401] 12.63442 12.65399 12.67451 12.69580 12.71767 12.73995 12.76245 12.78499
## [409] 12.80739 12.82946 12.85103 12.87190 12.89191 12.91086 12.92857 12.94487
## [417] 12.95957 12.97248 12.98343 12.99224 12.99902 13.00413 13.00768 13.00980
## [425] 13.01061 13.01023 13.00880 13.00643 13.00324 12.99937 12.99494 12.99006
## [433] 12.98487 12.97949 12.97404 12.96864 12.96195 12.95267 12.94107 12.92738
## [441] 12.91186 12.89474 12.87628 12.85673 12.83632 12.81532 12.79396 12.77249
## [449] 12.75117 12.73022 12.70991 12.69049 12.67219 12.65526 12.63995 12.62355
## [457] 12.60351 12.58041 12.55477 12.52715 12.49809 12.46815 12.43787 12.40780
## [465] 12.37848 12.35047 12.32430 12.30053 12.27971 12.26238 12.24908 12.23729
## [473] 12.22422 12.21010 12.19513 12.17953 12.16351 12.14728 12.13107 12.11507
## [481] 12.09951 12.08460 12.07055 12.05757 12.04588 12.03568 12.02721 12.02065
## [489] 12.01624 12.01418 12.01389 12.01461 12.01628 12.01885 12.02226 12.02646
## [497] 12.03137 12.03696 12.04316 12.04992 12.05718 12.06488 12.07296 12.08138
## [505] 12.09006 12.09897 12.10970 12.12376 12.14087 12.16077 12.18321 12.20793
## [513] 12.23467 12.26316 12.29315 12.32438 12.35658 12.38950 12.42288 12.45646
## [521] 12.48997 12.52316 12.55577 12.58754 12.61821 12.64751 12.67519 12.70100
## [529] 12.72466 12.74591 12.76451 12.78019 12.79540 12.81260 12.83153 12.85195
## [537] 12.87359 12.89620 12.91953 12.94331 12.96730 12.99124 13.01488 13.03795
## [545] 13.06021 13.08140 13.10126 13.11954 13.13599 13.15035 13.16236 13.17177
## [553] 13.17832 13.18177 13.18185 13.17902 13.17404 13.16705 13.15823 13.14773
## [561] 13.13571 13.12233 13.10775 13.09215 13.07566 13.05847 13.04072 13.02258
## [569] 13.00421 12.98577 12.96743 12.94933 12.93165 12.91454 12.89504 12.87051
## [577] 12.84163 12.80906 12.77346 12.73551 12.69587 12.65522 12.61421 12.57353
## [585] 12.53382 12.49577 12.46005 12.42731 12.39823 12.37347 12.35002 12.32461
## [593] 12.29752 12.26903 12.23943 12.20899 12.17800 12.14675 12.11551 12.08456
## [601] 12.05419 12.02468 11.99632 11.96938 11.94415 11.92091 11.89994 11.88153
## [609] 11.86595 11.85169 11.83711 11.82235 11.80753 11.79277 11.77822 11.76398
## [617] 11.75019 11.73698 11.72447 11.71280 11.70208 11.69245 11.68403 11.67695
## [625] 11.67134 11.66670 11.66250 11.65875 11.65548 11.65274 11.65054 11.64893
## [633] 11.64792 11.64756 11.64787 11.64889 11.65064 11.65315 11.65647 11.66061
## [641] 11.66561 11.67150 11.67831 11.68607 11.69492 11.70495 11.71611 11.72836
## [649] 11.74165 11.75595 11.77120 11.78737 11.80440 11.82226 11.84090 11.86027
## [657] 11.88033 11.90104 11.92235 11.94422
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 660)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.93742 11.93646 11.93553 11.93461 11.93371 11.93282 11.93194 11.93107
##   [9] 11.93020 11.92934 11.92847 11.92759 11.92671 11.92582 11.92491 11.92398
##  [17] 11.92304 11.92206 11.92107 11.92004 11.91898 11.91788 11.91674 11.91556
##  [25] 11.91433 11.91306 11.91173 11.91035 11.90891 11.90741 11.90584 11.90421
##  [33] 11.90250 11.90072 11.89887 11.89693 11.89492 11.89281 11.89062 11.88834
##  [41] 11.88596 11.88348 11.88090 11.87815 11.87518 11.87202 11.86866 11.86514
##  [49] 11.86145 11.85763 11.85369 11.84964 11.84549 11.84127 11.83699 11.83266
##  [57] 11.82830 11.82393 11.81956 11.81521 11.81089 11.80662 11.80241 11.79829
##  [65] 11.79426 11.79034 11.78655 11.78290 11.77941 11.77610 11.77298 11.77006
##  [73] 11.76736 11.76491 11.76270 11.76076 11.75911 11.75776 11.75613 11.75365
##  [81] 11.75040 11.74643 11.74180 11.73657 11.73080 11.72456 11.71789 11.71088
##  [89] 11.70356 11.69601 11.68828 11.68044 11.67254 11.66465 11.65682 11.64912
##  [97] 11.64160 11.63433 11.62736 11.62076 11.61459 11.60890 11.60376 11.59923
## [105] 11.59537 11.59224 11.58989 11.58839 11.58781 11.58819 11.58960 11.59210
## [113] 11.59575 11.60071 11.60703 11.61462 11.62337 11.63319 11.64399 11.65566
## [121] 11.66810 11.68123 11.69493 11.70911 11.72369 11.73854 11.75359 11.76873
## [129] 11.78386 11.79889 11.81372 11.82824 11.84237 11.85601 11.86905 11.88363
## [137] 11.90168 11.92282 11.94670 11.97294 12.00115 12.03098 12.06204 12.09397
## [145] 12.12638 12.15892 12.19120 12.22285 12.25350 12.28278 12.31031 12.33572
## [153] 12.35864 12.37870 12.39886 12.42205 12.44779 12.47563 12.50509 12.53572
## [161] 12.56704 12.59860 12.62992 12.66054 12.69000 12.71784 12.74357 12.76675
## [169] 12.78691 12.80358 12.81861 12.83413 12.85002 12.86619 12.88256 12.89901
## [177] 12.91546 12.93182 12.94797 12.96384 12.97932 12.99432 13.00874 13.02249
## [185] 13.03547 13.04758 13.05873 13.06883 13.07777 13.08547 13.09182 13.09674
## [193] 13.10011 13.10186 13.10188 13.10008 13.09637 13.09063 13.08214 13.07040
## [201] 13.05570 13.03834 13.01861 12.99680 12.97322 12.94815 12.92189 12.89473
## [209] 12.86696 12.83889 12.81080 12.78300 12.75576 12.72939 12.70418 12.68043
## [217] 12.65843 12.63508 12.60740 12.57584 12.54085 12.50290 12.46245 12.41993
## [225] 12.37582 12.33057 12.28462 12.23845 12.19250 12.14723 12.10310 12.06056
## [233] 12.02007 11.98208 11.94704 11.91543 11.88768 11.86427 11.84296 11.82134
## [241] 11.79951 11.77757 11.75566 11.73388 11.71233 11.69114 11.67042 11.65028
## [249] 11.63083 11.61219 11.59446 11.57777 11.56222 11.54793 11.53555 11.52552
## [257] 11.51762 11.51163 11.50731 11.50445 11.50282 11.50220 11.50236 11.50308
## [265] 11.50414 11.50532 11.50638 11.50711 11.50728 11.50668 11.50506 11.50222
## [273] 11.49793 11.49534 11.49733 11.50324 11.51242 11.52420 11.53794 11.55296
## [281] 11.56862 11.58426 11.59923 11.61285 11.62448 11.63347 11.63914 11.64086
## [289] 11.63795 11.63161 11.62359 11.61406 11.60319 11.59116 11.57814 11.56430
## [297] 11.54981 11.53485 11.51959 11.50421 11.48887 11.47375 11.45903 11.44487
## [305] 11.43144 11.41893 11.40750 11.39733 11.38521 11.36829 11.34726 11.32281
## [313] 11.29565 11.26645 11.23593 11.20477 11.17366 11.14331 11.11439 11.08762
## [321] 11.06368 11.04326 11.02707 11.01578 11.00698 10.99783 10.98846 10.97899
## [329] 10.96953 10.96022 10.95117 10.94250 10.93434 10.92680 10.92001 10.91408
## [337] 10.90914 10.90531 10.90271 10.90146 10.90168 10.90350 10.90703 10.91265
## [345] 10.92051 10.93039 10.94207 10.95532 10.96994 10.98569 11.00236 11.01972
## [353] 11.03756 11.05566 11.07379 11.09174 11.10928 11.12620 11.14227 11.15893
## [361] 11.17766 11.19831 11.22073 11.24476 11.27024 11.29703 11.32498 11.35392
## [369] 11.38371 11.41419 11.44521 11.47662 11.50826 11.53999 11.57164 11.60307
## [377] 11.63411 11.66463 11.69446 11.72346 11.75146 11.77832 11.80388 11.82800
## [385] 11.85051 11.87305 11.89720 11.92267 11.94920 11.97651 12.00434 12.03241
## [393] 12.06046 12.08821 12.11539 12.14173 12.16696 12.19082 12.21302 12.23330
## [401] 12.25138 12.26894 12.28766 12.30736 12.32782 12.34885 12.37026 12.39182
## [409] 12.41336 12.43466 12.45553 12.47576 12.49516 12.51351 12.53063 12.54631
## [417] 12.56036 12.57256 12.58272 12.59064 12.59615 12.59939 12.60059 12.59998
## [425] 12.59780 12.59429 12.58968 12.58421 12.57812 12.57165 12.56502 12.55847
## [433] 12.55225 12.54658 12.54171 12.53787 12.53347 12.52689 12.51836 12.50810
## [441] 12.49632 12.48324 12.46908 12.45405 12.43838 12.42229 12.40599 12.38969
## [449] 12.37362 12.35800 12.34304 12.32897 12.31599 12.30433 12.29420 12.28409
## [457] 12.27246 12.25953 12.24553 12.23067 12.21516 12.19924 12.18311 12.16699
## [465] 12.15110 12.13565 12.12088 12.10699 12.09420 12.08274 12.07281 12.06324
## [473] 12.05278 12.04158 12.02976 12.01747 12.00483 11.99199 11.97907 11.96622
## [481] 11.95357 11.94126 11.92941 11.91817 11.90767 11.89805 11.88943 11.88197
## [489] 11.87578 11.87101 11.86671 11.86192 11.85678 11.85144 11.84604 11.84072
## [497] 11.83563 11.83091 11.82669 11.82313 11.82037 11.81854 11.81779 11.81826
## [505] 11.82010 11.82345 11.82852 11.83535 11.84380 11.85377 11.86512 11.87774
## [513] 11.89149 11.90626 11.92192 11.93834 11.95542 11.97301 11.99100 12.00926
## [521] 12.02767 12.04611 12.06445 12.08257 12.10035 12.11766 12.13437 12.15038
## [529] 12.16554 12.17974 12.19286 12.20477 12.21759 12.23334 12.25170 12.27237
## [537] 12.29501 12.31933 12.34502 12.37175 12.39922 12.42712 12.45513 12.48294
## [545] 12.51024 12.53672 12.56205 12.58595 12.60808 12.62813 12.64581 12.66078
## [553] 12.67274 12.68139 12.68640 12.68885 12.69009 12.69020 12.68924 12.68728
## [561] 12.68442 12.68070 12.67622 12.67105 12.66525 12.65891 12.65210 12.64489
## [569] 12.63735 12.62956 12.62160 12.61354 12.60544 12.59740 12.58717 12.57286
## [577] 12.55498 12.53406 12.51064 12.48525 12.45842 12.43067 12.40254 12.37456
## [585] 12.34726 12.32116 12.29680 12.27471 12.25542 12.23946 12.22458 12.20833
## [593] 12.19089 12.17243 12.15316 12.13325 12.11288 12.09225 12.07154 12.05094
## [601] 12.03062 12.01078 11.99160 11.97326 11.95596 11.93987 11.92518 11.91208
## [609] 11.90076 11.89014 11.87910 11.86776 11.85622 11.84457 11.83293 11.82140
## [617] 11.81008 11.79908 11.78851 11.77846 11.76904 11.76036 11.75253 11.74564
## [625] 11.73980 11.73462 11.72968 11.72500 11.72058 11.71645 11.71264 11.70916
## [633] 11.70603 11.70327 11.70090 11.69894 11.69742 11.69635 11.69575 11.69564
## [641] 11.69604 11.69698 11.69846 11.70052 11.70324 11.70667 11.71079 11.71557
## [649] 11.72097 11.72696 11.73353 11.74063 11.74825 11.75634 11.76488 11.77384
## [657] 11.78319 11.79291 11.80296 11.81331
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")